package com.the4man.framework.action;

import com.opensymphony.xwork2.ModelDriven;
import com.the4man.framework.cache.PermissionCache;
import com.the4man.framework.model.DataGrid;
import com.the4man.framework.model.Moudle;
import com.the4man.framework.pagemodel.PMenu;
import com.the4man.framework.pagemodel.PPage;
import com.the4man.framework.pagemodel.PageContext;
import com.the4man.framework.service.MenuServiceI;
import com.the4man.framework.service.MoudleServiceI;
import com.the4man.framework.util.PageJson;
import com.the4man.framework.utils.RequestUtil;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.List;

/**
 * Created by lenovo on 2017/12/12.
 */
@Action(
        value = "menuAction",
        results = {@Result(
                name = "menu",
                location = "/framework/menu/menu.jsp"
        )}
)
public class MenuAction extends BaseAction implements ModelDriven<PMenu> {
    private static final long serialVersionUID = 1874844543837638288L;
    private static final Logger logger = LoggerFactory.getLogger(MenuAction.class);
    private String moudleid;
    PMenu pMenu = new PMenu();
    @Autowired
    public MenuServiceI menuService;
    @Autowired
    public MoudleServiceI moudleService;
    @Autowired
    public PermissionCache permissionCache;

    public MenuAction() {
    }

    public void getAllMenu() {
        this.writeJson(this.menuService.getAllMenuByMoudleID(this.moudleid));
    }

    public String getMoudleid() {
        return this.moudleid;
    }

    public void setMoudleid(String moudleid) {
        this.moudleid = moudleid;
    }

    public String executeMenuResult() {
        return "menu";
    }

    public void getAllMenuForPage() {
        PPage page = new PPage();
        String key = "com.the4man.framework.dao.MenuMapper.selectAllMenusForPage";
        PageContext.getContext().putSession(key, page);
        BeanUtils.copyProperties(this.pMenu, page);
        DataGrid d = new DataGrid();
        d.setRows(this.menuService.getAllMenu());
        d.setTotal(Long.valueOf(page.getTotals()));
        PageContext.getContext().removeSession(key);
        this.writeJson(d);
    }

    public void deleteMenu() {
        PageJson json = new PageJson();

        try {
            boolean b = this.menuService.deleteMenu(this.pMenu);
            this.permissionCache.refreshNeedToVerifyPermissions();
            if(b) {
                json.setReturnMsg("删除菜单成功！");
                json.setReturnFlag(true);
                this.writeJson(json);
            } else {
                json.setReturnMsg("删除菜单失败！");
                json.setReturnFlag(false);
                this.writeJson(json);
            }
        } catch (Exception var3) {
            logger.error("删除菜单数据出错" + var3.toString());
            json.setReturnMsg("删除菜单失败！");
            json.setReturnFlag(false);
        }

    }

    public void menuEdit() {
        PageJson json = new PageJson();

        try {
            boolean b = this.menuService.menuEdit(this.pMenu);
            this.permissionCache.refreshNeedToVerifyPermissions();
            if(b) {
                json.setReturnMsg("修改菜单成功！");
                json.setReturnFlag(true);
                this.writeJson(json);
            } else {
                json.setReturnMsg("修改菜单失败！");
                json.setReturnFlag(false);
                this.writeJson(json);
            }
        } catch (Exception var3) {
            logger.error("修改菜单出错" + var3.toString());
            json.setReturnMsg("修改菜单失败！");
            json.setReturnFlag(false);
        }

    }

    public void menulist() {
        String roleid = RequestUtil.getSessionUserinfo().getRoleid();
        this.writeJson(this.menuService.getTreeMenu(roleid));
    }

    public void addMenu() {
        PageJson json = new PageJson();

        try {
            this.pMenu.setUserid(RequestUtil.getSessionUserinfo().getUserid());
            int isuccess = this.menuService.insertMenu(this.pMenu);
            this.permissionCache.refreshNeedToVerifyPermissions();
            json.setObj(this.pMenu);
            json.setReturnFlag(true);
            json.setReturnMsg("保存成功！");
        } catch (Exception var3) {
            json.setReturnFlag(false);
            json.setReturnMsg("菜单保存失败出错了");
        }

        this.writeJson(json);
    }

    public void getMoudle() {
        try {
            List<Moudle> moudles = this.moudleService.getMoudlesByRoleID(RequestUtil.getSessionUserinfo().getRoleid());
            this.writeJson(moudles);
        } catch (Exception var2) {
            logger.error("获取当前用户所有模块数据" + var2.toString());
        }

    }

    public void setLocationState() {
        HttpServletRequest request = this.getRequest();
        int status = Integer.parseInt(request.getParameter("state"));
        File file = new File("1.txt");
        if(status == 1) {
            if(!file.exists()) {
                try {
                    file.createNewFile();
                } catch (IOException var5) {
                    var5.printStackTrace();
                }
            }
        } else if(file.exists()) {
            file.delete();
        }

        PageJson json = new PageJson();
        json.setReturnMsg("修改状态成功！");
        json.setReturnFlag(true);
        this.writeJson(json);
    }

    public void selectLocationState() {
        String message = "";
        File file = new File("1.txt");
        if(!file.exists()) {
            message = "定位服务未开启";
        } else if(file.exists()) {
            message = "定位服务已开启";
        }

        PageJson json = new PageJson();
        json.setReturnMsg(message);
        json.setReturnFlag(true);
        this.writeJson(json);
    }

    public void getAllMenuAdd() {
        List<PMenu> menus = this.menuService.getAllMenuByMoudleID(this.moudleid);
        Moudle Moudle = this.moudleService.findMoudleBykey(this.moudleid);
        PMenu pMenu = new PMenu();
        pMenu.setId(this.moudleid);
        pMenu.setText(Moudle.getMoudlename());
        List<PMenu> moudleByMoudleId = this.menuService.getAllMenuByMoudleID(this.moudleid);
        PMenu p = new PMenu();
        p.setId(Moudle.getMoudleid());
        p.setText(Moudle.getMoudlename());
        moudleByMoudleId.add(p);
        this.writeJson(moudleByMoudleId);
    }

    public PMenu getModel() {
        return this.pMenu;
    }
}
